package com.example.transport.message;

public class ResponseFuture {

    private String id;
    private Response response;
    private volatile boolean isSucceed = false;
    private final Object object = new Object();

    public Response getRpcResponse() {
        synchronized (object) {
            while (!isSucceed) {
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return response;
        }
    }

    public void setResponse(Response response) {
        if (isSucceed) {
            return;
        }
        synchronized (object) {
            this.response = response;
            this.isSucceed = true;
            object.notify();
        }
    }

    public ResponseFuture(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Response getResponse() {
        return response;
    }

    public boolean isSucceed() {
        return isSucceed;
    }

    public void setSucceed(boolean succeed) {
        isSucceed = succeed;
    }

    public Object getObject() {
        return object;
    }
}
